package com.kaizeli.website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaizeli.website.mapper.CustomerMapper;
import com.kaizeli.website.pojo.dto.base.PageDTO;
import com.kaizeli.website.pojo.entity.CustomerDO;
import com.kaizeli.website.pojo.vo.CustomerVO;
import com.kaizeli.website.query.CustomerQuery;
import com.kaizeli.website.service.CustomerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CustomerServiceImpl implements CustomerService {
    
    private final CustomerMapper customerMapper;
    
    @Override
    public PageDTO<CustomerVO> pageQuery(CustomerQuery query) {
        Page<CustomerDO> page = new Page<>(query.getPageNum(), query.getPageSize());
        
        QueryWrapper<CustomerDO> queryWrapper = new QueryWrapper<>();
        if (query.getName() != null && !query.getName().isEmpty()) {
            queryWrapper.like("name", query.getName());
        }
        if (query.getCode() != null && !query.getCode().isEmpty()) {
            queryWrapper.eq("code", query.getCode());
        }
        if (query.getStatus() != null && !query.getStatus().isEmpty()) {
            queryWrapper.eq("status", query.getStatus());
        }
        queryWrapper.orderByDesc("id");
        
        Page<CustomerDO> result = customerMapper.selectPage(page, queryWrapper);
        return PageDTO.of(result, CustomerVO.class);
    }
    
    @Override
    public CustomerVO getByCode(String code) {
        QueryWrapper<CustomerDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        CustomerDO customerDO = customerMapper.selectOne(queryWrapper);
        if (customerDO == null) {
            return null;
        }
        return convertToVO(customerDO);
    }
    
    @Override
    public Integer save(CustomerDO customerDO) {
        customerMapper.insert(customerDO);
        return customerDO.getId();
    }
    
    @Override
    public CustomerVO getById(Integer id) {
        CustomerDO customerDO = customerMapper.selectById(id);
        if (customerDO == null) {
            return null;
        }
        return convertToVO(customerDO);
    }
    
    @Override
    public Boolean update(CustomerDO customerDO) {
        return customerMapper.updateById(customerDO) > 0;
    }
    
    @Override
    public Boolean deleteById(Integer id) {
        return customerMapper.deleteById(id) > 0;
    }
    
    @Override
    public List<CustomerVO> listByStatus(String status) {
        List<CustomerDO> list = customerMapper.selectList(
            new QueryWrapper<CustomerDO>().eq("status", status)
        );
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public List<CustomerVO> listAll() {
        List<CustomerDO> list = customerMapper.selectList(null);
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public List<CustomerVO> search(String keyword) {
        QueryWrapper<CustomerDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", keyword).or().like("code", keyword);
        List<CustomerDO> list = customerMapper.selectList(queryWrapper);
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    /**
     * 转换为VO
     */
    private CustomerVO convertToVO(CustomerDO customerDO) {
        CustomerVO vo = new CustomerVO();
        vo.setId(customerDO.getId());
        vo.setName(customerDO.getName());
        vo.setCode(customerDO.getCode());
        vo.setStatus(customerDO.getStatus());
        return vo;
    }
}

